﻿using System;
using System.Collections;
using System.Collections.Generic;
//using PHSExtSave;
using Studio;
using UnityEngine;

namespace WzhAddon
{
	public class AdvancedIKMgr : BaseMgr<AdvancedIKMgr>
	{
		private Studio.Studio studio;
		public List<AdvancedIKController> activeControllers = new List<AdvancedIKController>();		

		private void InstallIKNodeController()
		{
			this.studio = Singleton<Studio.Studio>.Instance;
			if (this.studio == null) return;

			try
			{
				foreach (ObjectCtrlInfo objectCtrlInfo in this.studio.dicObjectCtrl.Values)
				{
					if (objectCtrlInfo is OCIChar)
					{
						OCIChar ocichar = objectCtrlInfo as OCIChar;
						this.TryInstallIKNodeController(ocichar, ocichar.charInfo);
					}
				}
			}
			catch (Exception arg)
			{
				Console.WriteLine("Error while Install IKNodeContainer: {0}", arg);
			}
		}


		private void TryInstallIKNodeController(OCIChar studioChara, ChaControl chara)
		{
			if (chara.gameObject == null) return;
			if (chara.gameObject.GetComponent<AdvancedIKController>() != null) return;

			chara.gameObject.AddComponent<AdvancedIKController>().Init(studioChara);
		}

		public void PlayBaseAnimAllChara()
		{
			foreach (AdvancedIKController advancedIKController in this.activeControllers)
			{
				if (advancedIKController.gameObject == null) continue;
				advancedIKController.studioChara.charAnimeCtrl.animator.speed = advancedIKController.studioChara.animeSpeed;
			}
		}

		public void PauseBaseAnimAllChara()
		{
			foreach (AdvancedIKController advancedIKController in this.activeControllers)
			{
				if (advancedIKController.gameObject == null) continue;
				advancedIKController.studioChara.charAnimeCtrl.animator.speed = 0f;
			}
		}

		public void ResetBaseAnimAllChara()
		{
			foreach (AdvancedIKController advancedIKController in this.activeControllers)
			{
				if (advancedIKController.gameObject == null) continue;
				advancedIKController.studioChara.RestartAnime();
			}
		}

		public static AdvancedIKController GetCurrentCharaController()
		{
			Studio.Studio instance = Singleton<Studio.Studio>.Instance;
			GuideObject selectObject = Singleton<GuideObjectManager>.Instance.selectObject;
			if (selectObject == null) return null;
			ObjectCtrlInfo objectCtrlInfo = instance.dicObjectCtrl[selectObject.dicKey];
			if (objectCtrlInfo is OCIChar)
			{
				return AdvancedIKController.Install(objectCtrlInfo as OCIChar);
			}
			return null;
		}

	
	}
}
